//
//  ESWeiXinNewsDetailViewController.m
//  EasyStyle
//
//  Created by FK on 16/1/11.
//  Copyright © 2016年 FK. All rights reserved.
//

#import "ESWeiXinNewsDetailViewController.h"
#import "ESBaseWebView.h"
#import "ESWeChatCollectionDAO.h"

@interface ESWeiXinNewsDetailViewController ()<WKNavigationDelegate, WKUIDelegate>

@property (nonatomic, strong)ESBaseWebView *webView;

@property (nonatomic, strong)UIButton *collectionButton;

@property (nonatomic, strong)UILabel *collectionTipsView;

@end

@implementation ESWeiXinNewsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupRootView];
    
    [self loadData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- 布局相关
- (void)setupRootView
{
    NSInteger startX = 0;
    
    NSInteger endX = self.view.frame.size.width;
    NSInteger endY = self.view.frame.size.height;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBarHidden = YES;
    [self addCustomNavigationBarWithStyle:DefaultStyle customColor:[UIColor whiteColor]];
    WS(weakSelf);
    [self.sgCustomNavigationBar showBackButtonWithActionBlock:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    
    [self.sgCustomNavigationBar setTitle:_newsTitle];
    self.sgCustomNavigationBar.titleLabel.textColor = [UIColor blackColor];
    startX += self.sgCustomNavigationBar.height;

    // webView
    _webView = [[ESBaseWebView alloc]initWithFrame:CGRectMake(0, startX, ScreenWidth, ScreenHeight-startX)];
    _webView.backgroundColor = [UIColor whiteColor];
    _webView.UIDelegate = self;
    _webView.navigationDelegate = self;
    [self.view addSubview:_webView];

    // 收藏tips
    _collectionTipsView =  [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    _collectionTipsView.backgroundColor = [UIColor lightGrayColor];
    _collectionTipsView.alpha = 0;
    _collectionTipsView.layer.cornerRadius = 4.0f;
    _collectionTipsView.clipsToBounds = YES;
    _collectionTipsView.center = self.view.center;
    _collectionTipsView.textAlignment = NSTextAlignmentCenter;
    _collectionTipsView.textColor = [UIColor whiteColor];
    [self.view addSubview:_collectionTipsView];
    
    
    // 目前只有两处进入本页需要显示收藏：微信精选列表页 和 阅读历史夜
    if (_newsObject && _isCanCollect) {

        // 收藏
        _collectionButton = [[UIButton alloc] initWithFrame:CGRectMake(endX - 80, endY - 80, 50, 50)];
        
        _collectionButton.backgroundColor= [UIColor colorWithHex:@"e7e3d8"];
        _collectionButton.layer.cornerRadius = _collectionButton.height / 2;

        [_collectionButton setImage:[UIImage imageNamed:@"article_collection_ic01"] forState:UIControlStateNormal];
        [_collectionButton setImage:[UIImage imageNamed:@"article_collection_ic01"] forState:UIControlStateDisabled];
        [_collectionButton setImage:[UIImage imageNamed:@"article_collection_ic02"] forState:UIControlStateSelected];
        [_collectionButton setImage:[UIImage imageNamed:@"article_collection_ic02"] forState:UIControlStateHighlighted];
        
        [_collectionButton addTarget:self action:@selector(addCollection:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_collectionButton];
        
        
        ESWeChatCollectionDAO *dao = [ESWeChatCollectionDAO shareInstance];
        if ([dao queryExistedData:_newsObject]) {
            _collectionButton.selected = YES;
        }
        
    }
}

#pragma mark -- loadData
- (void)loadData
{
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_newsURL]]];
}

#pragma mark -- 事件函数
- (void)addCollection:(UIButton *)sender
{
    ESWeChatCollectionDAO *dao = [ESWeChatCollectionDAO shareInstance];
    
    if (sender.selected) {
        // 移除收藏
        _collectionButton.selected = NO;
        if ([dao removeSingleData:_newsObject]) {
            // 取消收藏tips
            _collectionTipsView.text = @"取消收藏";
        }
    } else {
        // 添加收藏
        _collectionButton.selected = YES;
        if ([dao insertCollectionData:_newsObject]) {
            // 收藏成功tips
            _collectionTipsView.text = @"收藏成功";
        }
    }
    
    [UIView animateWithDuration:1 animations:^{
        _collectionTipsView.alpha = 0.8;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.8 animations:^{
            _collectionTipsView.alpha = 0;
        } completion:^(BOOL finished) {
        }];
    }];
    
}


@end
